package com.daxt.controller.system;


import com.daxt.common.annotations.LoginAuth;
import com.daxt.common.annotations.LoginUser;
import com.daxt.mapper.archives.ArchivesBehaveLogMapper;
import com.daxt.mapper.sys.ArchivesFondsMapper;
import com.daxt.model.service.archives.vo.ArchivesBehaveLog;
import com.daxt.model.service.archives.vo.ArchivesFonds;
import com.daxt.model.service.base.result.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daxt.common.result.PageInfo;
import com.daxt.common.result.PageUtil;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.sys.OperationLogMapper;
import com.daxt.model.service.base.vo.OperationLog;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "操作日志")
@RestController
public class OperationLogController {

		
	@Autowired
	private OperationLogMapper operationLogMapper;
	@Autowired
	private ArchivesFondsMapper archivesFondsMapper;

	@Autowired
	private ArchivesBehaveLogMapper archivesBehaveLogMapper;

	@ApiOperation(value = "操作列表", notes = "") 
	@GetMapping("/operationLog/{type}")
	@LoginAuth()
	public Result<PageInfo<OperationLog>> list(
			@PathVariable("type")String type,
			@ApiParam("") @RequestParam(value = "pageNum", required = false) Integer pageNum,
			@ApiParam("") @RequestParam(value = "pageSize", required = false) Integer pageSize,
			@ApiParam("") @RequestParam(value = "fondsId", required = false) String fondsId,
			@ApiIgnore @LoginUser UserInfo loginUser){
		if (pageNum == null || pageSize == null) {
			pageNum = 1;
			pageSize = 50;
		}
		Page<OperationLog> page = new Page<>(pageNum, pageSize);
		QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("otype", type);

		if(!"admin".equals(loginUser.getUsername()) && !"超级管理员".equals(loginUser.getRole()) ){
			queryWrapper.eq("fondsId",loginUser.getFondsId());
		}else{
			queryWrapper.eq(StringUtils.isNotEmpty(fondsId),"fondsId",fondsId);
		}
		queryWrapper.orderByDesc("createTime");
		IPage<OperationLog> info = operationLogMapper.selectPage(page, queryWrapper);

		for(int i=0;i<info.getRecords().size();i++){
			OperationLog log=info.getRecords().get(i);
			String fId = log.getFondsId();
			if(StringUtils.isNotEmpty(fId)){
				ArchivesFonds fonds = archivesFondsMapper.selectById(fId);
				if(fonds!=null && StringUtils.isNotEmpty(fonds.getFondsName())) info.getRecords().get(i).setFondsName(fonds.getFondsName());
			}
		}
		PageInfo<OperationLog> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(), info.getRecords());
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", data);
	}

	@ApiOperation(value = "档案行为日志", notes = "")
	@GetMapping("/behaveLog")
	@LoginAuth()
	public Result<PageInfo<ArchivesBehaveLog>> behaveLogList(
			@ApiParam("") @RequestParam(value = "pageNum", required = false) Integer pageNum,
			@ApiParam("") @RequestParam(value = "pageSize", required = false) Integer pageSize,
			@ApiParam("") @RequestParam(value = "fondsId", required = false) String fondsId,
			@ApiIgnore @LoginUser UserInfo loginUser){
		if (pageNum == null || pageSize == null) {
			pageNum = 1;
			pageSize = 50;
		}
		Page<ArchivesBehaveLog> page = new Page<>(pageNum, pageSize);
		QueryWrapper<ArchivesBehaveLog> queryWrapper = new QueryWrapper<>();


		if(!"admin".equals(loginUser.getUsername()) && !"超级管理员".equals(loginUser.getRole()) ){
			queryWrapper.eq("fondsId",loginUser.getFondsId());
		}else{
			queryWrapper.eq(StringUtils.isNotEmpty(fondsId),"fondsId",fondsId);
		}
		queryWrapper.orderByDesc("createTime");
		IPage<ArchivesBehaveLog> info = archivesBehaveLogMapper.selectPage(page, queryWrapper);

		for(int i=0;i<info.getRecords().size();i++){
			ArchivesBehaveLog log=info.getRecords().get(i);
			String fId = log.getFondsId();
			if(StringUtils.isNotEmpty(fId)){
				ArchivesFonds fonds = archivesFondsMapper.selectById(fId);
				if(fonds!=null && StringUtils.isNotEmpty(fonds.getFondsName())) info.getRecords().get(i).setFondsName(fonds.getFondsName());
			}
		}
		PageInfo<ArchivesBehaveLog> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(), info.getRecords());
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", data);
	}

}
